import Mock from 'mockjs'
import { HTTP_SUCCESS_CODE } from '@/constants'

const timeout = 1000

// 模拟菜单数据
const menuData = [
  {
    menuId: 1,
    menuParentId: 0,
    menuName: '系统管理',
    menuCode: 'SYSTEM_MANAGE',
    menuRoute: '/system',
    menuType: '0', // 0-目录 1-菜单 2-按钮
    menuIcon: 'vi-carbon:settings',
    ordinal: 1,
    isEnable: 1,
    remark: '系统管理模块'
  },
  {
    menuId: 2,
    menuParentId: 1,
    menuName: '菜单管理',
    menuCode: 'MENU_MANAGE',
    menuRoute: '/system/menu',
    menuType: '1',
    menuIcon: 'vi-carbon:menu',
    ordinal: 1,
    isEnable: 1,
    remark: '菜单管理功能'
  },
  {
    menuId: 3,
    menuParentId: 1,
    menuName: '用户管理',
    menuCode: 'USER_MANAGE',
    menuRoute: '/system/user',
    menuType: '1',
    menuIcon: 'vi-carbon:user',
    ordinal: 2,
    isEnable: 1,
    remark: '用户管理功能'
  },
  {
    menuId: 4,
    menuParentId: 1,
    menuName: '角色管理',
    menuCode: 'ROLE_MANAGE',
    menuRoute: '/system/role',
    menuType: '1',
    menuIcon: 'vi-carbon:user-role',
    ordinal: 3,
    isEnable: 1,
    remark: '角色管理功能'
  }
]

export default [
  // 获取菜单列表
  {
    url: '/mock/system/menu/list',
    method: 'get',
    timeout,
    response: () => {
      return {
        code: HTTP_SUCCESS_CODE,
        data: {
          list: menuData
        }
      }
    }
  },
  // 获取菜单详情
  {
    url: '/mock/system/menu/detail/:id',
    method: 'get',
    timeout,
    response: ({ query }: any) => {
      const id = parseInt(query.id)
      const menu = menuData.find(item => item.menuId === id)
      return {
        code: HTTP_SUCCESS_CODE,
        data: menu || null
      }
    }
  },
  // 新增菜单
  {
    url: '/mock/system/menu/add',
    method: 'post',
    timeout,
    response: ({ body }: any) => {
      const newMenu = {
        ...body,
        menuId: Mock.Random.integer(100, 999)
      }
      menuData.push(newMenu)
      return {
        code: HTTP_SUCCESS_CODE,
        data: newMenu
      }
    }
  },
  // 更新菜单
  {
    url: '/mock/system/menu/update/:id',
    method: 'put',
    timeout,
    response: ({ query, body }: any) => {
      const id = parseInt(query.id)
      const index = menuData.findIndex(item => item.menuId === id)
      if (index !== -1) {
        menuData[index] = { ...menuData[index], ...body }
        return {
          code: HTTP_SUCCESS_CODE,
          data: menuData[index]
        }
      }
      return {
        code: 400,
        message: '菜单不存在'
      }
    }
  },
  // 删除菜单
  {
    url: '/mock/system/menu/delete/:id',
    method: 'delete',
    timeout,
    response: ({ query }: any) => {
      const id = parseInt(query.id)
      const index = menuData.findIndex(item => item.menuId === id)
      if (index !== -1) {
        menuData.splice(index, 1)
        return {
          code: HTTP_SUCCESS_CODE,
          message: '删除成功'
        }
      }
      return {
        code: 400,
        message: '菜单不存在'
      }
    }
  }
] 